/**
 * Created on Nov 25, 2003
 */
package com.unash.util;

/**
 * @author Ashwini Gokhale
 */
import java.io.IOException;
import java.io.InputStream;

public class ByteArray implements Consts{
	private byte[] value;
		
	public ByteArray(byte[] value) 
		   throws IllegalArgumentException 
	{
		if ((null == value) || (ZERO >= value.length))
			throw new IllegalArgumentException();
		this.value = value;
	} //ByteArray(byte[] value)
	
	
	public static ByteArray binaryValue(int value){
		byte[] resultRev = new byte[10];
		int pos = ZERO;
		while (0xFF < Math.abs(value)) {
			resultRev[pos] = (byte) (value & 0xFF);
			value = value >> 8;
			pos++;
		} //while

		resultRev[pos] = (byte) value;
		byte[] result = new byte[pos + ONE];
		int j = pos;
		for (int i = ZERO; i <= pos; i++)
			result[i] = resultRev[j--];
		
		return new ByteArray(result);
		
	}//
	
	
	//TODO:Throw userdefined exception instead of IOException
	public ByteArray(InputStream bs,int noBytes)
		throws IOException, IllegalArgumentException{
			if(ZERO >= noBytes)
				throw new IllegalArgumentException();
			value = AsnUtil.readNBytes(bs,noBytes);
	}//ByteArray
	
		
	public boolean equals(Object obj) {
		if (!(obj instanceof ByteArray))
			return false;
		ByteArray objByteArray = (ByteArray) obj;
		if (value.length != objByteArray.value.length)
			return false;
	
		for (int i = ZERO; i < value.length; i++) {
			if (value[i] != objByteArray.value[i])
				return false;
		} //for
		return true;
	} //equals


	public int hashCode() {
		int result = 17;
		
		for(int i=0;i<value.length;i++){
			result = 37 * result + value[i]; 
		}//for
 
		return result;
	} //hashCode


	/*public boolean isBitSet(int index)
		throws IllegalArgumentException
	{
		if((0 >= index) || (value.length*BYTE_SIZE) < index)
			throw new IllegalArgumentException();
	
		//get the index in the byte array
		int byteIndex = (index-1)/BYTE_SIZE;
	
		//find the distance for shifting value to right
		int noBitRShift = (byteIndex+1) * BYTE_SIZE - index;
	
		//RightShift	
		int rShiftValue = value[byteIndex] >> noBitRShift;
		
		return((rShiftValue & MASK) == 1);	
	}//getBit*/
	
	
	/**
	 * This function returns the integer value of the byte specified by the index.
	 * param index : (0 to ByteArray size-1) 
	 */
	public int toInt(int index) throws IllegalArgumentException{
		if((ZERO > index) || (index > value.length - ONE))
			throw new IllegalArgumentException();
		return (int)value[index];
	}//toInt(byte value)
	
	
	/**
	 * This function returns the byte specified by the index.
	 * @param index : (0 to ByteArray size-1)
	 * @return byte 
	*/
	public byte getByte(int index) throws IllegalArgumentException{
		if((ZERO > index) || (index > value.length - ONE))
			throw new IllegalArgumentException();
		return value[index];
	}//getByte(int index)
	
	
	public String toString(){
		StringBuffer byteArrString = new StringBuffer();
			
		for(int i=ZERO;i<value.length;i++){
			for(int j=ONE;j<=BYTE_SIZE;j++){
				if(AsnUtil.isBitSet(value[i],j))
					byteArrString.append(STR_ONE);
				else
					byteArrString.append(STR_ZERO);
			}//for(int j=1;j<=8;j++)
		}//for(int i=0;i<value.length;i++)
	
		return byteArrString.toString();
	
	}//toString()
	
	
	
	/**
	 * @return
	 */
	public byte[] getValue() {
		return value;
	}

}//ByteArray